package org.fujene.term;

import org.fujene.structure.Term;
import org.fujene.structure.TermList;

public class BigramChopper implements Chopper{
	private static final String stopWord = " 　！!＄$％%’\'（(）)、,。.・／/￥\\；;：:「[{」]}＠@＜<＞>～~";

	@Override
	public TermList chop(String content){
		TermList terms = new TermList(content.length());

		CharSequence subst = null;
		for(int i = -1; i < content.length(); i++){
			if (i == -1)
				subst = content.substring(i + 1, i + 2);
			else if (i == content.length() - 1)
				subst = content.substring(i, i + 1);
			else subst = content.substring(i, i + 2);
			int strlen = subst.length();

			if (strlen == 1 && !stopWord.contains(subst)){
				terms.add(new Term(subst.toString()));
			}else if (strlen == 2){
				if (!stopWord.contains(subst.subSequence(1, 2))
						&& !stopWord.contains(subst.subSequence(0, 1))){
					terms.add(new Term(subst.toString()));
				}else if (stopWord.contains(subst.subSequence(0, 1))){
					terms.add(new Term(subst.subSequence(1, 2).toString()));
				}else if (stopWord.contains(subst.subSequence(1, 2))){
					terms.add(new Term(subst.subSequence(0, 1).toString()));
				}
			}
		}

		return terms;
	}

}
